//表格初始化
function initTable(){

    $("#table").bootstrapTable({
        method:"post",
        contentType:"application/json",
        url:"/emp",
        //设置交替行样式
        striped:true,
        //初始显示页数
        pageNum:1,
        //是否显示分页
        pagination:true,
        //设置是服务端分页还是客户端分页
        sidePagination:"server",
        //页大小
        pageSize:5,
        //分页列表
        pageList: [3,5,6,10],
        //上传到服务器的参数
        queryParams:function (data) {
            return{
                //计算页号，当前号
                pageNum:(data.offset/data.limit)+1,
                //计算页大小
                pageSize: data.limit,
                searchName:$("#searchName").val(),
                searchGender:$("#searchGender").val(),
                minBirth:$("#minBirth").val(),
                maxBirth:$("#maxBirth").val(),
                searchDeptId:$("#searchDeptId").val()
            }
        },
        columns:[
            // {title:"ID",field:"id",align:"center"},
            {title:"员工编号",field:"empNum",align:"center"},
            {title:"姓名",field:"empName",align:"center"},
            {title:"性别",field:"gender",align:"center",formatter:function (value,row,index) {
                    if(value==0){
                        return "男";
                    }
                    if(value==1){
                        return "女";
                    }
                }},
            {title:"生日",field:"birthday",align:"center"},
            {title:"电话",field:"phone",align:"center"},
            {title:"部门",field:"dept.deptName",align:"center"},
            {title:"操作",align: "center",field: "id",formatter:function (value,row,index) {
                    var delBtn="<button class='btn btn-warning delBtn' onclick='delEmpBtn("+value+")'  >删除</button>";
                    var updBtn="<button class='btn btn-primary updBtn' onclick='update("+JSON.stringify(row)+")'>修改</button>";
                    return delBtn+"  "+updBtn;
                }},
        ]

    })



}

function delEmpBtn(id){
    if(confirm("你确定要删除吗？")){
        $.ajax({
            url:"emp/deleteEmp",
            type: "post",
            data: "id="+id,
            success:function () {
                $('#table').bootstrapTable('refresh');
            }
        })
    }
}
function update(obj) {
    // alert(obj);
    if(typeof (obj)=="undefined" || obj=="undefined" ){
        //添加
        $("#info").text("添加员工信息");
        $("#updateEmpBtn").attr("id","insertEmpBtn");
        $("#birthday").attr("type","date");
        $("#gender option:first-child").css("display","block");
        $("#updateEmpForm")[0].reset();
        $("#updateModel").modal("show");

    }else {
        // alert("xiugai");
        //修改
        $("#updateModel").modal("show");
        $("#info").text("修改员工信息");
        $("#birthday").attr("type","text");
        $("#gender option:first-child").css("display","none");
        $("#insertEmpBtn").attr("id","updateEmpBtn");

        $("#id").val(obj.id);
        $("#empNum").val(obj.empNum);
        $("#empName").val(obj.empName);
        $("#gender").val(obj.gender);
        $("#birthday").val(obj.birthday);
        $("#phone").val(obj.phone);
        $("#address").val(obj.address);
        $("#duty").val(obj.duty);
        $("#deptId").val(obj.deptId);

    }
}

$(function () {
    initTable();
    $("#searchBtn").click(function () {
        $('#table').bootstrapTable('refresh');
    })
    $(document).on("click","#insertEmpBtn,#updateEmpBtn",function () {
        var url;
        if($(this).attr("id")=="insertEmpBtn"){
            url="emp/insertEmp";
        }else{
            url="emp/updateEmp";
        }
        $.ajax({
            url:url,
            type:"post",
            data:$("#updateEmpForm").serialize(),
            success:function () {
                $("#updateModel").modal("hide");
                $('#table').bootstrapTable('refresh');
            }
        })
    })

})